package com.iuie.ns.system.beans;

import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.utils.DateUtils;
import com.iuie.ns.frame.utils.StringUtils;

class ReportMonthParams extends ReportParams {

	public ReportMonthParams(String statisticsSize, String statisticRange, String startTime, String endTime) {
		this.endTime = endTime;
		this.startTime = startTime;
		this.statisticSize = statisticsSize;
		this.statisticRange = statisticRange;
		this.range = Integer.parseInt(statisticRange);
	}

	@Override
	protected ReportParams init() {
		// if startTime is null
		//	 	if endTime is null
		//	 		endTime = currentTime, range not changed
		//	 	if endTime is not null
		//	 		if endTime > currentTime
		//	 			endTime = currentTime, range not changed
		//	 		if endTime <= currentTime
		//	 			no opt
			// if startTime is not null
		//	 	if startTime > currentTime
		//	 		throw exception
		//	 	if startTime <= currentTime
		//	 		if endTime is null
		//	 			endTime = startTime + range
		//	 			if endTime > currentTime
		//	 				endTime = currentTime
		//	 				range = endTime - startTime
		//	 			if endTime <= currentTime
		//	 				no opt
		//	 		if endTime is not null
		//	 			if endTime >= currentTime
		//	 				endTime = currentTime
		//	 				range = endTime - startTime
		//	 			if endTime < currentTime
		//	 				range = endTime - startTime
		final String PATTERN = "yyyy-MM";
		String currentDate = DateUtils.getCurrentDate(PATTERN);
		if (StringUtils.isBlank(this.startTime)) {
			if (StringUtils.isBlank(this.endTime) || DateUtils.compareDate(this.endTime, currentDate) == 3) {
				this.endTime = currentDate;
			}
		} else {
			if (DateUtils.compareDate(this.startTime, currentDate) == 3) {
				throw new ServiceException("开始月份须小于当前月份");
			} else {
				if (StringUtils.isBlank(this.endTime)) {
					this.endTime = DateUtils.addDate(this.startTime, this.range, PATTERN);
					if (DateUtils.compareDate(this.endTime, currentDate) == 3) {
						this.endTime = currentDate;
						this.range = DateUtils.getMonthsTween(this.endTime, this.startTime);
					}
				} else {
					int cq = DateUtils.compareDate(this.endTime, currentDate);
					if (cq == 3 || cq == 2) {
						this.endTime = currentDate;
					}
					this.range = DateUtils.getMonthsTween(this.endTime, this.startTime);
				}
			}
		}
		
		String month = DateUtils.addMonth(this.endTime, -(this.range - 1), PATTERN);
		this.startTime = month + "-01";
		
		this.compute();
		return this;
	}

	private void compute() {
		final String PATTERN = "yyyy-MM";
		
		// range 须大于0
		if (this.range <= 0) {
			throw new ServiceException("开始时间须早于结束时间，或开始时间应早于今天【" + DateUtils.getCurrentDate() + "】");
		}
		if (this.range > 60) {
			throw new ServiceException("按月统计只能统计60个月以内的数据！");
		}
		
		// 往回倒数 statisticsRange 月
		for (int i = this.range - 1; i >= 0; i--) {
			String qmonth = DateUtils.addMonth(this.endTime, -i, PATTERN); // 用于数据库查询的date
			String month = DateUtils.formatDate(qmonth, PATTERN, "yyyy年MM月"); // 用于显示的标签
			
			queryParams.add(qmonth);
			categories.add(month);
		}
	}

}
